<?php

/**
 * Created by PhpStorm.
 * User: zuoa
 * Date: 2016/12/28 0028
 * Time: 10:26
 */
class Accident extends Y_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 统计信息
     */
    public function statistics()
    {
        $this->check_login();
        $data["usage"] = $this->user_model->get_usage_statistics();
        $data["dept_statistics"] = $this->record_model->get_process_statistics_by_department();
        $data["police_statistics"] = $this->record_model->get_process_statistics_by_police();
        $data["today_police_statistics"] = $this->record_model->get_today_process_statistics_by_police(0);
        $data['userdept'] = $this->session->userdata('userdept');
        $data['is_admin'] = $this->session->userdata('is_admin');

        $this->load->view('dashboard/header', $this->_get_context());
        $this->load->view('dashboard/statistics', $data);
        $this->load->view('dashboard/footer');

    }

    public function adjust()
    {
        $this->check_login();

        if ($this->is_post()) {
            $related = $this->input->post("related");
            $adjust_related = $this->input->post("adjust_related");
            $this->record_model->adjust($related, $adjust_related);
            $this->response(200, "success", null);
        } else {
            $search = $this->input->get("s");
            $data = null;
            if (!empty($search)) {
                $data["search_result"] = $this->record_model->search_record($search);
            }

            $this->load->view('dashboard/header', $this->_get_context());
            $this->load->view('dashboard/accident/adjust', $data);
            $this->load->view('dashboard/footer');
        }
    }

    public function live()
    {
        $this->check_login();
        $this->load->view('history_template');
    }

    public function json()
    {
        $this->check_login();

        $type = $this->input->get("t");

        switch ($type) {
            case 'proc_data_by_day': {
                $num = $this->input->get("n");
                if (!is_int($num) || $num == 0) {
                    $num = 10;
                }

                $arr = $this->record_model->get_proc_data_by_day($num);
                $max = 8;
                $axis = array();
                $data = array();
                for ($i = 0; $i < count($arr); $i++) {
                    $axis[$i] = $arr[$i]["day"];
                    $data[$i] = $arr[$i]["count"];
                    if ($data[$i] > $max) {
                        $max = $data[$i];
                    }
                }

                $max = (int)($max * 5 / 4);

                $result["axis"] = $axis;
                $result["data"] = $data;
                $result["max"] = $max;

                $this->response(200, "success", $result);


                break;
            }

            case 'proc_data_by_week': {
                $num = $this->input->get("n");
                if (!is_int($num) || $num == 0) {
                    $num = 10;
                }

                $arr = $this->record_model->get_proc_data_by_week($num);
                $max = 8;
                $axis = array();
                $data = array();
                for ($i = 0; $i < count($arr); $i++) {
                    $axis[$i] = $arr[$i]["week"];
                    $data[$i] = $arr[$i]["count"];
                    if ($data[$i] > $max) {
                        $max = $data[$i];
                    }
                }

                $max = (int)($max * 5 / 4);

                $result["axis"] = $axis;
                $result["data"] = $data;
                $result["max"] = $max;

                $this->response(200, "success", $result);


                break;
            }

            case 'proc_data_by_month': {
                $num = $this->input->get("n");
                if (!is_int($num) || $num == 0) {
                    $num = 10;
                }

                $arr = $this->record_model->get_proc_data_by_month($num);
                $max = 8;
                $axis = array();
                $data = array();
                for ($i = 0; $i < count($arr); $i++) {
                    $axis[$i] = $arr[$i]["month"];
                    $data[$i] = $arr[$i]["count"];
                    if ($data[$i] > $max) {
                        $max = $data[$i];
                    }
                }

                $max = (int)($max * 5 / 4);

                $result["axis"] = $axis;
                $result["data"] = $data;
                $result["max"] = $max;

                $this->response(200, "success", $result);


                break;
            }
            case "today": {
                $num = $this->input->get("n");
                if (!is_int($num) || $num == 0) {
                    $num = 10;
                }

                $arr = $this->record_model->get_today_data_by_department($num);
                $result["data"] = $arr;

                $this->response(200, "success", $result);
                break;
            }
            default: {
                echo '{}';
                break;
            }
        }
    }


    /**
     * @param $category 待定位或待录入
     */
    public function analysis($category)
    {
        $this->check_login();

        if ($this->is_post()) {
            if ($category == "locate") {
                $id = $this->input->post("id");
                $this->record_model->ignore_locate($id);
                $this->response(200, "success", null);

            } else if ($category == "typein") {
                $id = $this->input->post("id");
                $this->record_model->ignore_typein($id);
                $this->response(200, "success", null);
            }

        } else {

            $this->load->view('dashboard/header', $this->_get_context());
            $page = $this->uri->segment(4, 1);
            $per_page_num = 10;
            $config['base_url'] = 'http://sghd.yuy.im/accident/analysis/' . $category;
            $config['per_page'] = $per_page_num;
            $config['full_tag_open'] = '<ul class="pagination pagination-sm">';
            $config['full_tag_close'] = '</ul>';

            $config['anchor_class'] = "";

            $config['cur_tag_open'] = '<li class="active"><a>';
            $config['cur_tag_close'] = '</a></li>';

            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';

            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';

            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';

            $config['first_link'] = '首页';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';

            $config['last_link'] = '尾页';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';

            $config['prev_link'] = '上一页';
            $config['next_link'] = '下一页';

            $config['use_page_numbers'] = TRUE;


            if ($category == "locate") {
                $config['total_rows'] = $this->record_model->get_require_locate_num();
                $this->pagination->initialize($config);

                $data["pagination"] = $this->pagination->create_links();
                $data["records"] = $this->record_model->get_require_locate_records(($page - 1) * $per_page_num, $per_page_num);

                $this->load->view('dashboard/analysis/locate', $data);
            } else if ($category == "typein") {
                $config['total_rows'] = $this->record_model->get_require_type_in_num();
                $this->pagination->initialize($config);

                $data["pagination"] = $this->pagination->create_links();
                $data["records"] = $this->record_model->get_require_type_in_records(($page - 1) * $per_page_num, $per_page_num);

                $this->load->view('dashboard/analysis/typein', $data);
            } else {
                $config['total_rows'] = $this->record_model->get_complete_count();
                $this->pagination->initialize($config);

                $data["pagination"] = $this->pagination->create_links();
                $data["records"] = $this->record_model->get_complete_records(($page - 1) * $per_page_num, $per_page_num);

                $this->load->view('dashboard/analysis/complete', $data);
            }

            $this->load->view('dashboard/footer');
        }
    }


    /**
     * 按单位显示
     * @param $dept
     */
    public function dlist($dept)
    {

        $this->check_login();

        $per_page_num = 10;
        $page = $this->uri->segment(4, 1);
        $config['base_url'] = 'http://sghd.yuy.im/accident/dlist/' . $dept;
        $config['per_page'] = $per_page_num;
        $config['full_tag_open'] = '<ul class="pagination pagination-sm">';
        $config['full_tag_close'] = '</ul>';

        $config['anchor_class'] = "";

        $config['cur_tag_open'] = '<li class="active"><a>';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';

        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';

        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = '尾页';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';

        $config['use_page_numbers'] = TRUE;

        $config['total_rows'] = $this->record_model->get_month_count(urldecode($dept));
        $this->pagination->initialize($config);
        $data["pagination"] = $this->pagination->create_links();


        $records = $this->record_model->get_page_records(urldecode($dept), ($page - 1) * $per_page_num, $per_page_num);
        $data["records"] = $records;

        $this->load->view('dashboard/header', $this->_get_context());
        $this->load->view('dashboard/record/list', $data);
        $this->load->view('dashboard/footer');
    }


    /**
     * 按警员显示
     * @param $police
     */
    public function plist($police)
    {

        $this->check_login();
        $per_page_num = 10;
        $page = $this->uri->segment(4, 1);
        $config['base_url'] = 'http://sghd.yuy.im/accident/plist/' . $police;
        $config['per_page'] = $per_page_num;
        $config['full_tag_open'] = '<ul class="pagination pagination-sm">';
        $config['full_tag_close'] = '</ul>';

        $config['anchor_class'] = "";

        $config['cur_tag_open'] = '<li class="active"><a>';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';

        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';

        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = '尾页';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';

        $config['use_page_numbers'] = TRUE;

        $config['total_rows'] = $this->record_model->get_month_count(urldecode($police));
        $this->pagination->initialize($config);
        $data["pagination"] = $this->pagination->create_links();


        $records = $this->record_model->get_page_records(urldecode($police), ($page - 1) * $per_page_num, $per_page_num);
        $data["records"] = $records;

        $this->load->view('dashboard/header', $this->_get_context());
        $this->load->view('dashboard/record/list', $data);
        $this->load->view('dashboard/footer');
    }

}